package xin.ssm.jxc.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import xin.ssm.jxc.model.GoodsPurchase;
import xin.ssm.jxc.services.IPurchaseService;
import xin.ssm.jxc.util.Result;

@Controller
@RequestMapping("/purchase")
public class PurchaseController {
	@Resource
	private IPurchaseService purchaseService;
	
		// 查询订单
		@RequestMapping("/findpurchasedynamic")
		@ResponseBody
		public Result findPurchaseDynamic(
				@RequestParam(value = "purchaseid") String purchaseid,
				@RequestParam(value = "employeecode") String employeecode,
				@RequestParam(value = "companyid") String companyid,
				@RequestParam(value = "goodsid") String goodsid,
				@RequestParam(value = "starttime") String starttime,
				@RequestParam(value = "endtime") String endtime,
				@RequestParam(value = "purchasestatu") String purchasestatu) {
			
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			if (purchaseid.equals("") || purchaseid.isEmpty()) {
				purchaseid = "0";
			}
			if (employeecode.equals("") || employeecode.isEmpty()) {
				employeecode = null;
			}
			if (companyid.equals("") || companyid.isEmpty()) {
				companyid = "0";
			}
			if (goodsid.equals("") || goodsid.isEmpty()) {
				goodsid = "0";
			}
//			if (starttime.equals("") || starttime.isEmpty()) {
//				starttime = "1980-01-01";
//				try {
//					start=sdf.parse(starttime);
//				} catch (ParseException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				
//			}
//			if (endtime.equals("") || endtime.isEmpty()) {
//				endtime = "2029-12-31";
//				try {
//					end=sdf.parse(endtime);
//				} catch (ParseException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//
//			}
//			try {
//				start=sdf.parse(starttime);
//				end=sdf.parse(endtime);
//			} catch (ParseException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
			if (purchasestatu.equals("") || purchasestatu.isEmpty()) {
				purchasestatu = null;
			}
			GoodsPurchase gp = new GoodsPurchase();
			gp.setPurchaseid(Integer.parseInt(purchaseid));
			gp.setEmployeecode(employeecode);
			gp.setCompanyid(Integer.parseInt(companyid));
			gp.setGoodsid(Integer.parseInt(goodsid));
			try {
				gp.setStarttime(sdf.parse(starttime));
				gp.setStarttime(sdf.parse(endtime));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			gp.setPurchasestatu(purchasestatu);
			List<GoodsPurchase> gpList = purchaseService.findGoodsPurchaseDynamic(gp);
			
			// 时间转换
//			String jsonarry = JSONArray.toJSONStringWithDateFormat(gpList, "yyyy-MM-dd HH:mm:ss");
			Result result = new Result();
			result.setCode(0);
			result.setMsg("成功");
			result.setCount(gpList.size());
			result.setData(gpList);
			return result;
		}
		
		// 删除订单
				@RequestMapping("/deletepurchase")
				@ResponseBody
				public String deletePurchase(@RequestParam(value = "purchaseid") int purchaseid) {
					int ret = purchaseService.deletePurchase(purchaseid);
					if (ret > 0) {
						return "1";
					} else {
						return "0";
					}
				}
			//增加
				@RequestMapping("/insertpurchase")
				@ResponseBody
				public String insertPurchase(
						@RequestParam(value = "purchaseprice") BigDecimal purchaseprice,
						@RequestParam(value = "purchasenum") Integer purchasenum,
						@RequestParam(value = "employeecode") String employeecode,
						@RequestParam(value = "companyid") Integer companyid,
						@RequestParam(value = "goodsid") Integer goodsid) {// 类别id
					GoodsPurchase gp = new GoodsPurchase();
					gp.setPurchaseprice(purchaseprice);
					gp.setPurchasenum(purchasenum);
					gp.setEmployeecode(employeecode);
					gp.setCompanyid(companyid);
					gp.setGoodsid(goodsid);
					int ret = purchaseService.addPurchase(gp);
					if (ret > 0) {
						return "1";
					} else {
						return "0";
					}
				}
				
				//修改
				@RequestMapping("/updatepurchase")
				@ResponseBody
				public String updatePurchase(
						@RequestParam(value = "purchaseid") Integer purchaseid,
						@RequestParam(value = "purchaseprice") BigDecimal purchaseprice,
						@RequestParam(value = "purchasenum") Integer purchasenum,
						@RequestParam(value = "purchasesum") BigDecimal purchasesum,
						@RequestParam(value = "employeecode") String employeecode,
						@RequestParam(value = "companyid") Integer companyid,
						@RequestParam(value = "goodsid") Integer goodsid) {// 类别id
					GoodsPurchase gp = new GoodsPurchase();
					gp.setPurchaseid(purchaseid);
					gp.setPurchaseprice(purchaseprice);
					gp.setPurchasenum(purchasenum);
					gp.setPurchasesum(purchasesum);
					gp.setEmployeecode(employeecode);
					gp.setCompanyid(companyid);
					gp.setGoodsid(goodsid);
					int ret = purchaseService.updatePurchase(gp);
					if (ret > 0) {
						return "1";
					} else {
						return "0";
					}
				}
}